package com.kevin.chat.server.handler;

import com.kevin.chat.server.message.MessageProcess;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;

/*
 * 处理webSocket请求连接
 * @author Kevin
 * @date 2021/1/13 14:56
 *
 */
public class WebSocketHandler extends SimpleChannelInboundHandler<TextWebSocketFrame> {
    /*消息处理器*/
    private static MessageProcess messageProcess = new MessageProcess();

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, TextWebSocketFrame msg) throws Exception {
        System.out.println("channelRead0.....");
        messageProcess.process(ctx, msg);
    }

    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
        System.out.println("handlerAdded...");
        //用户上线
        messageProcess.login(ctx);
    }

    @Override
    public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
        System.out.println("handlerRemoved ...");
        //用户上线
        messageProcess.logout(ctx);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        System.err.println("异常信息--》" + cause.getMessage());
    }
}
